package com.jixiantubu.jinnang;

import com.example.jixiantubu.R;
import com.jixiantubu.bean.Sciencebean;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class JinNnang_TuiJian_JuTiNeiRong extends Activity implements
		OnClickListener {
	TextView textView;// 科普推荐具体内容
	String aString;// 接受上一个界面传递过来的参数
	ImageView backImageView, shoucangImageView;// 创建后退键和收藏
	Intent intent;// 定义意图
	boolean flag = true;// 判断是否收藏
	String url = "http://" + MyApplication.getIP()
			+ ":8080/Jinnang/ScienceService";
	Sciencebean sciencebean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.jinnang_kepu_tuijian_jutineirong);
		initViews();
		initData();
	}

	private void initViews() {
		Intent intent = getIntent();
		sciencebean = (Sciencebean) intent.getSerializableExtra("neirong");
		Log.e("hujia11111", sciencebean.toString());
		textView = (TextView) findViewById(R.id.jinnnang__kepu_tuijian__jutineirong_tv);
		textView.setMovementMethod(ScrollingMovementMethod.getInstance());
		backImageView = (ImageView) findViewById(R.id.JinNang_keputuijian_jutineirong_back);
		shoucangImageView = (ImageView) findViewById(R.id.jinnang_keputuijian_jutineirong_shoucang);
		shoucangImageView.setImageResource(R.drawable.quxiaoshoucang);
		mySetOnClickListener(shoucangImageView, backImageView);
	}

	private void initData() {
		textView.setText(sciencebean.getS_content());
		getwebcheckshoucang(url);
	}

	private void mySetOnClickListener(View... views) {
		for (View view : views) {
			view.setOnClickListener(this);
		}

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.JinNang_keputuijian_jutineirong_back:
			finish();
			break;
		case R.id.jinnang_keputuijian_jutineirong_shoucang:
			// 如果flag为真，显示为已收藏
			if (!flag) {
				shoucangImageView.setImageResource(R.drawable.shoucang);
				Log.e("hjj", "已收藏");
				getwebshoucang(url);
				flag = true;
				// 如果flag为假，显示为未收藏
			} else {
				shoucangImageView.setImageResource(R.drawable.quxiaoshoucang);
				getwebqushaoshoucang(url);
				Log.e("hjj", "未收藏");
				flag = false;
			}
			break;
		default:
			break;
		}
	}

	// 查看web端科普锦囊是否被收藏
	private void getwebcheckshoucang(String url) {
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", "3");
		params.addBodyParameter("user_id", MyApplication.user_id);
		String s_id = sciencebean.getS_id() + "";
		params.addBodyParameter("s_id", s_id);
		params.addBodyParameter("c","3");
		MyApplication.httpclient.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						
						String result = arg0.result;
						Log.e("1111DDD#@!!SS", result);
						if (result.equals("没有收藏")) {
							flag = false;
							shoucangImageView
									.setImageResource(R.drawable.quxiaoshoucang);
						}else {
							flag=true;
							shoucangImageView
							.setImageResource(R.drawable.shoucang);
						}
					}
				});
	}

	// 收藏
	private void getwebshoucang(String url) {
		RequestParams params1 = new RequestParams();
		params1.addBodyParameter("key", "4");
		params1.addBodyParameter("user_id", MyApplication.user_id);
		String s_id = sciencebean.getS_id() + "";
		params1.addBodyParameter("s_id", s_id);
		params1.addBodyParameter("c","1");
		Log.e("信息已发送", "转台改变通知");
		MyApplication.httpclient.send(HttpMethod.POST, url, params1,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Log.e("sc", "失败");

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Log.e("sc", "成功收藏");
						String result = arg0.result;
						MyApplication.show(JinNnang_TuiJian_JuTiNeiRong.this,
								result);

					}
				});
	}

	// 取消收藏
	private void getwebqushaoshoucang(String url) {
		RequestParams params2 = new RequestParams();
		params2.addBodyParameter("key", "5");
		params2.addBodyParameter("user_id", MyApplication.user_id);
		String s_id = sciencebean.getS_id() + "";
		params2.addBodyParameter("s_id", s_id);
		params2.addBodyParameter("c","2");
		MyApplication.httpclient.send(HttpMethod.POST, url, params2,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String result = arg0.result;
						MyApplication.show(JinNnang_TuiJian_JuTiNeiRong.this,
								result);

					}
				});
	}
}
